package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.activities.CatalogWithBookActivity;
import me.shurufa.model.BookInfo;
import me.shurufa.model.BookInfoResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;

/**
 * 点击图书 获取垂直滑动页面的图书信息部分
 *
 * @FileName: me.shurufa.fragments.AboutBookInfoFragment.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-22 11:05
 */
public class AboutBookInfoFragment extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.book_img)
    ImageView bookImg;
    @Bind(R.id.book_title)
    TextView bookTitle;
    @Bind(R.id.book_author)
    TextView bookAuthor;
    @Bind(R.id.book_press)
    TextView bookPress;

    @Bind(R.id.watch_catalog)
    RelativeLayout watchCatalog;
    @Bind(R.id.description_view)
    TextView descriptionView;
    @Bind(R.id.expand_view)
    ImageView expandView;
    @Bind(R.id.description_layout)
    LinearLayout descriptionLayout;

    private long bookId;
    //最多显示5行
    int maxDescripLine = 5;
    //是否展开
    boolean isExpand;

    private BookInfo bookInfo;


    public static AboutBookInfoFragment newInstance(long bookId) {
        AboutBookInfoFragment fragment = new AboutBookInfoFragment();
        Bundle args = new Bundle();
        args.putLong(Constants.ARG_BOOK_ID, bookId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            bookId = getArguments().getLong(Constants.ARG_BOOK_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_about_bookinfo, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        watchCatalog.setOnClickListener(this);
        expandView.setOnClickListener(this);
        initData();
    }

    private void initData() {
        new RequestServerTask<BookInfoResponse>(BookInfoResponse.class) {

            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<>();
                params.put("bookId", bookId);
                return HttpUtil.post(NetworkConstants.GET_BOOK_INFO, params);
            }

            @Override
            protected void onSuccess(BookInfoResponse result) {
                bookInfo = result.data;
                if (!TextUtils.isEmpty(result.data.image))
                    Glide.with(getActivity()).load(result.data.image).into(bookImg);
                if (!TextUtils.isEmpty(result.data.author))
                    bookAuthor.setText(result.data.author);
                if (!TextUtils.isEmpty(result.data.title))
                    bookTitle.setText(result.data.title);
                if (!TextUtils.isEmpty(result.data.publisher))
                    bookPress.setText(result.data.publisher);
                if (!TextUtils.isEmpty(result.data.description)) {
                    descriptionView.setText(result.data.description);
                    descriptionView.setHeight(descriptionView.getLineHeight() * maxDescripLine);
                    descriptionView.post(new Runnable() {
                        @Override
                        public void run() {
                            expandView.setVisibility(descriptionView.getLineCount() > maxDescripLine ? View.VISIBLE : View.GONE);

                        }
                    });
                }
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.watch_catalog:
                Intent intent = new Intent(getActivity(), CatalogWithBookActivity.class);
                intent.putExtra(Constants.ARG_BOOK_ID,bookId);
                if (bookInfo!=null) {
                    intent.putExtra(Constants.ARG_BOOK_INFO, bookInfo.toString());
                }
                startActivity(intent);
                break;
            case R.id.expand_view: {
                isExpand = !isExpand;
                descriptionView.clearAnimation();
                final int deltaValue;
                final int startValue = descriptionView.getHeight();
                int durationMillis = 350;
                if (isExpand) {
                    deltaValue = descriptionView.getLineHeight() * descriptionView.getLineCount() - startValue;
                    RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    expandView.startAnimation(animation);
                } else {
                    deltaValue = descriptionView.getLineHeight() * maxDescripLine - startValue;
                    RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    expandView.startAnimation(animation);
                }
                Animation animation = new Animation() {
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        descriptionView.setHeight((int) (startValue + deltaValue * interpolatedTime));
                    }

                };
                animation.setDuration(durationMillis);
                descriptionView.startAnimation(animation);
            }
            break;
        }
    }
}
